package webservice;

import entities.Admin;
import java.util.Calendar;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.json.Json;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import sessions.IUserAccountSession;
import util.Clock;
import util.Validator;

/**
 *
 * @author a21210380
 */
@WebService(name = "TimeWebService", serviceName = "TimeWebService")
public class TimeWebService {

    @EJB
    private Clock clock;
    @Inject
    private IUserAccountSession userAccountSession;

    @WebMethod(operationName = "setTime")
    public void setTime(@WebParam(name = "date") String date) {
        if (!userAccountSession.isVisitor()) {
            Admin admin = userAccountSession.getAdmin();
            if (admin != null) {
                clock.setTime(Validator.parseDate(date));
            }
        }
    }

    @WebMethod(operationName = "getTime")
    public String getTime() {
        Calendar calendar = clock.getCalendar();
        return Json.createObjectBuilder()
                .add("year", calendar.get(Calendar.YEAR))
                .add("month", calendar.get(Calendar.MONTH))
                .add("day", calendar.get(Calendar.DATE))
                .add("hour", calendar.get(Calendar.HOUR))
                .add("minutes", calendar.get(Calendar.MINUTE))
                .add("seconds", calendar.get(Calendar.SECOND))
                .build()
                .toString();
    }
}
